package com.epro.dx.fragment;

import android.annotation.SuppressLint;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.epro.dx.R;
import com.epro.dx.widget.MyWebView;
import com.weipu.common.util.GlobalConfig;

/************************
 * @author Tony
 * @Date:2016-4-19上午10:29:17
 * @描述:产品详情页的图文详情展示
 ************************/
@SuppressLint("ValidFragment")
public class ProductDetaiDescriptionFragment extends Fragment {
	
	private String html;
	private View rootView;
	private MyWebView webView;
	
	public ProductDetaiDescriptionFragment(){};
	public ProductDetaiDescriptionFragment(String html){
		this.html = html;
	};
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		if(null!=savedInstanceState){
			this.html = savedInstanceState.getString("html");
		}
		if(rootView == null){
			rootView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_product_detai_description, null);
		}
		ViewGroup parent = (ViewGroup) rootView.getParent();
		if(null!=parent){
			parent.removeView(rootView);
		}
		if(!TextUtils.isEmpty(html)){
			initWebview();
		}
		return rootView;
	}
	@Override
	public void onSaveInstanceState(Bundle outState) {
		outState.putString("html", html);
		super.onSaveInstanceState(outState);
	}
	/**
	 * @创建：Tony 2016-4-19上午10:58:05
	 * @描述：设置加载网页
	 */
	@SuppressLint("SetJavaScriptEnabled")
	private void initWebview()
    {
		webView = (MyWebView) rootView.findViewById(R.id.product_detail_webview);
        webView.getSettings().setSupportZoom(false);
        webView.getSettings().setBuiltInZoomControls(false);
        webView.getSettings().setJavaScriptEnabled(true);
        //自适应屏幕
        webView.getSettings().setUseWideViewPort(true); 
        webView.getSettings().setLoadWithOverviewMode(true);

        webView.setWebViewClient(new WebViewClient()
        {
            //跳过HTTPS验证
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error)
            {
            	if(GlobalConfig.getInstance().isCheckSSL()){
            		handler.proceed();
            	}else{
            		handler.cancel();
            	}
            }
            
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
            	view.loadUrl(url); 
            	return super.shouldOverrideUrlLoading(view, url);
            }
            
        });
        webView.setWebChromeClient(new WebChromeClient()
        {
            @Override
            public void onProgressChanged(WebView view, int newProgress)
            {
            }
        });
        if(!TextUtils.isEmpty(html)){
        	webView.loadDataWithBaseURL(null,html, "text/html", "utf-8", null);
        }
    }
}
